package LRU

import "container/list"

type TaskQueue struct {
}

type DataQueue struct {
	queueSize int
	datas     *list.List
	cache     map[string]*list.Element
	cap       int
}

func NewDataQueue(size int) *DataQueue {
	return &DataQueue{
		queueSize: size,
		cap:       0,
	}
}

func (d *DataQueue) AddData(key string, val string) string {
	if d.cap >= d.queueSize {
		return "The queue is up to max!"
	}
	entry := NewData(key, val)
	elem := d.datas.PushFront(entry)
	d.cache[key] = elem
	d.cap += 1
	return "Add successful!"
}

func (d *DataQueue) AccessData(key string) string {
	if d.cap == 0 {
		return "The queue has no data"
	}

	if elem, ok := d.cache[key]; ok {
		d.datas.MoveToFront(elem)
		return elem.Value.(*Entry).GetValue()
	}

	return "not found"
}

func (d *DataQueue) RemoveOldest(key string) string {
	if d.cap == 0 {
		return "The queue has no data"
	}
	elem := d.datas.Back()
	if elem != nil {
		entry := elem.Value.(*Entry)
		d.datas.Remove(elem)
		delete(d.cache, entry.value)
		d.cap -= 1
	}
	return "Delete Ok!"
}
